

package top.continew.admin.controller.question;

import org.springframework.web.bind.annotation.*;
import top.continew.admin.common.context.UserContextHolder;
import top.continew.admin.question.manger.service.model.*;
import top.continew.admin.question.manger.service.TestService;
import top.continew.starter.core.validation.ValidationUtils;
import top.continew.starter.web.model.R;

import java.util.List;

@RestController
@RequestMapping("test")
public class TestController {

    private TestService testService;

    public TestController(TestService testService) {
        this.testService = testService;
    }

    @GetMapping
    public R getList(TestParam testParam) throws Exception {
        return R.ok(testService.getList(testParam));
    }

    @GetMapping("spotCheck")
    public R getSpotCheckQuestionList(QuerySpotCheckTest querySpotCheckTest) throws Exception {
        return R.ok(testService.getSpotCheckTestList(querySpotCheckTest));
    }

    @GetMapping("spotCheckSum")
    public R getSpotCheckQuestionSum(QuerySpotCheckTest querySpotCheckTest) throws Exception {
        return R.ok(testService.getSpotCheckQuestionSum(querySpotCheckTest));
    }

    @PostMapping("{id}/type/{type}-id:{typeId}")
    public R bindClassOrCurriculum(@PathVariable long id,
                                   @PathVariable long type,
                                   @PathVariable long typeId) throws Exception {
        testService.bindClassOrCurriculum(id, type, typeId);
        return R.ok();
    }

    @PostMapping("{id}/type")
    public R batchBindClassOrCurriculum(@PathVariable long id, @RequestBody List<Value> values) throws Exception {
        testService.batchBindClassOrCurriculum(id, values);
        return R.ok();
    }

    @GetMapping("{id}/type")
    public R getClassOrCurriculum(@PathVariable long id) throws Exception {
        return R.ok(testService.getClassOrCurriculum(id));
    }

    @GetMapping("id:{id}")
    public R getTestInfo(@PathVariable long id) throws Exception {
        return R.ok(testService.getInfo(id));
    }

    @PostMapping
    public R addTest(@RequestBody AddUpTest addUpTest) throws Exception {
        return R.ok(testService.addUpTest(addUpTest));
    }

    @PostMapping("{id}/testStructure")
    public R saveTestStructure(@PathVariable long id,
                               @RequestParam String title,
                               @RequestParam double score,
                               @RequestParam(defaultValue = "-1") Long testStructureId) throws Exception {
        return R.ok(testService.saveTestStructure(id, testStructureId, title, score));
    }

    @PostMapping("{testId}/testStructure/{testStructureId}/question")
    public R addQuestion(@PathVariable Long testStructureId,
                         @PathVariable Long testId,
                         @RequestParam long questionId,
                         @RequestParam double score,
                         @RequestParam(value = "isUpdateProvenance", defaultValue = "0") int isUpdateProvenance) throws Exception {

        return R.ok(testService.saveQuestion(testStructureId, testId, questionId, score, isUpdateProvenance == 1));
    }

    @PutMapping("{testId}/testStructure/{testStructureId}/question/{questionId}")
    public R upQuestion(@PathVariable Long testStructureId,
                        @PathVariable Long testId,
                        @RequestParam long sort,
                        @PathVariable String questionId) throws Exception {
        testService.upQuestion(testStructureId, testId, questionId, sort);
        return R.ok();
    }

    @DeleteMapping("{testId}/testStructure/{testStructureId}/question/{questionId}")
    public R deleteTestQuestion(@PathVariable Long testStructureId,
                                @PathVariable Long testId,
                                @PathVariable String questionId) throws Exception {
        testService.deleteTestQuestion(testStructureId, testId, questionId);
        return R.ok();
    }

    @GetMapping("{id}/testStructure")
    public R getTestStructure(@PathVariable long id) throws Exception {
        return R.ok(testService.getTestStructure(id));
    }

    @DeleteMapping("{testId}/testStructure:{id}")
    public R deleteTestStructure(@PathVariable String id, @PathVariable String testId) throws Exception {
        boolean flag = !testService.verExistStructureQuestion(Long.parseLong(id));
        ValidationUtils.throwIf(flag, "该试卷结构存在的未删除的题目，请先删除对应题目");
        testService.deleteTestStructure(testId, id);
        return R.ok();
    }

    @PutMapping("{testId}/testStructure:{id}/sort")
    public R testStructureSort(@PathVariable String testId,
                               @PathVariable String id,
                               @RequestParam long sort) throws Exception {
        testService.testStructureSort(testId, id, sort);
        return R.ok();
    }

    @PutMapping("{testId}/commitCheck")
    public R commitCheck(@PathVariable String testId, Long checkPeople) throws Exception {
        testService.commitCheck(testId, checkPeople);
        return R.ok();
    }

    @PutMapping("{testId}/rejectCheck")
    public R rejectCheck(@PathVariable String testId, @RequestParam String msg) throws Exception {
        testService.rejectCheck(testId, msg);
        return R.ok();
    }

    @PutMapping("/rejectCheckMultiple")
    public R rejectCheckMultiple(@RequestBody RejectMultipleDto dto) throws Exception {
        testService.rejectCheckMultiple(dto.getIds(), dto.getMsg());
        return R.ok();
    }

    @PutMapping("{testId}/commitReview")
    public R commitReview(@PathVariable String testId, Long reviewPeople) throws Exception {
        boolean flag = !testService.verExistNoReviewQuestion(Long.parseLong(testId));
        ValidationUtils.throwIf(flag, "该试卷存在未提交审核的题目，请先提交对应题目进行审核");
        testService.commitReview(testId, reviewPeople);
        return R.ok();
    }

    @PutMapping("{testId}/rejectReview")
    public R rejectReview(@PathVariable String testId, @RequestParam String msg) throws Exception {
        testService.rejectReview(testId, msg);
        return R.ok();
    }

    @PutMapping("{testId}/review")
    public R review(@PathVariable String testId) throws Exception {
        testService.review(testId);
        return R.ok();
    }

    /**
     * 批量驳回审核接口
     *
     * @return
     * @throws Exception
     */

    @PostMapping(value = "/rejectReviewMultiple")
    public R rejectReviewMultiple(@RequestParam(value = "testIds") String testIds,
                                  @RequestParam(value = "msg", required = false) String msg) throws Exception {
        testService.rejectReviewMultiple(testIds, msg);
        return R.ok();
    }

    @DeleteMapping(value = "/delTest")
    public R delTest(@RequestBody DelQuery delQuery) throws Exception {
        ValidationUtils.throwIf(delQuery.getId() != null && delQuery.getId() > 0, "请选择需要试卷的题目");
        return R.ok(testService.delTest(delQuery.getId(), UserContextHolder.getUserId()));
    }
}
